#include "stdafx.h"
#include "luawindow.h"

#include "pane.h"
#include "directui.h"

#include <gtl/io/ostrm.h>
#include <gtl/directui/visitor.h>

namespace gtl
{

namespace dui
{

directui* luawindow::m_directui = NULL;

luawindow::luawindow()
{

} 
       
void luawindow::close(const char* id)
{

}

void luawindow::show(const char* id)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return;

	return pane_ptr->show();
}

void luawindow::hide(const char* id)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return;

	return pane_ptr->hide();
}

void luawindow::move(const char* id, int x, int y, int width, int height)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return;

	pane_ptr->move(gtl::rect_f(x, y, x + width, y + height));
}

bool luawindow::visible(const char* id)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return false;

	return pane_ptr->visible();
}

void luawindow::alert(const char* text, const char* caption)
{
	if(text == NULL || caption == NULL)
		return;

	HWND hWnd = NULL;
	gtl::str strCaption = caption;
	if(m_directui != NULL && m_directui->get_main_wnd() != NULL)
	{
		hWnd = m_directui->get_main_wnd();
		if(hWnd != NULL && (caption == NULL || *caption == 0))
		{
			char szCaption[MAX_PATH] = {0};
			::GetWindowTextA(hWnd, szCaption, MAX_PATH);
			strCaption = szCaption;
		}
	}

	::MessageBoxA(hWnd, text, strCaption, MB_OK);
}

std::string luawindow::attrib(const char* id, const char* attrb)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return 0;

#ifdef UNICODE
	return pane_ptr->attrib(gtl::tstr(attrb)).trans().c_str();
#else
	return pane_ptr->attrib(attrb).c_str();
#endif
}

unsigned long luawindow::hwnd(const char* id)
{
	return 0;
}

void luawindow::rect(const char* id)
{

}

int luawindow::width(const char* id)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return 0;

	return pane_ptr->rect().width;
}

int luawindow::height(const char* id)
{
	pane* pane_ptr = get_pane(id);
	if(pane_ptr == NULL)
		return 0;

	return pane_ptr->rect().width;
}

void luawindow::position(const char* id)
{

}

void luawindow::set_directui_ptr(directui* directui_ptr)
{
	m_directui = directui_ptr;
}

pane* luawindow::get_pane(const char* id)
{
	if(m_directui == NULL || id == NULL || *id == 0)
		return NULL;

#ifdef UNICODE
	return m_directui->get_pane(gtl::str(id).trans());
#else
	return m_directui->get_pane(id.c_str());
#endif
}

} // end of namespace dui

} // end of namespace gtl
